clear;
close all;
clc;


Proto_Num = 11;
% thre = 0.05;
thre = 0.02;
freqs= [300 600 1500 3000]; %just a random frequency
fs = 44100;
% out_table=f_P3_table;
    % tt=readtable(['Human_results\Subject' num2str(isub) '_P' num2str(Protocol) '_day1.xlsx'])
    tt=readtable(['Protocol_' num2str(Proto_Num) '.xlsx'])
    for i=1:4
        for j=1:30
            out_table(j,i)=tt{j,i};
        end
    end
whos
plot_each =1;
projection1 = 0; % neariest center
% projection1 = 1; % first project to the unit circle
Num_centers = 1;

true_angle = [-80 -65 -55 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 55 65 80];
Elevation_number = 9;


% Angles = 1:1;
Angles = 1:20;

conds = 'AB';
sides = {'Left'}; % fake for Protocol = 3

f2=figure('position',[400 70 500 500]);
for iangle = 1:length(Angles)
    cc = 0;
    f1=figure('position',[100 70 1200 500]);
    for icond = 1:1
        % eval(['load All_sound_recordings\Protocol_' num2str(Proto_Num) '_' sides{icond} '_'   num2str(Angles(iangle)) 'degree.mat']);
        eval(['load All_sound_recordings\Protocol_' num2str(Proto_Num) '.2_' sides{icond} '_'   num2str(Angles(iangle)) 'degree.mat']);

        left_sound = recorded_sound(:,1);
        right_sound = recorded_sound(:,2);
        for ifreql = 1:length(freqs)
            freq = freqs(ifreql)

            [SL,SR,freq_pos,flag]=mySTFT(left_sound,right_sound,fs,thre);
            % normalization:
            [SL1,SR1]=Normalize(SL,SR);
            [Y Ind] = min(abs(freq_pos - freq));
            X_P = [real(SR1(Ind,flag>0))' imag(SR1(Ind,flag>0))'];
            cc=cc+1;
            figure(f1)
            subplot(2,4,cc)
            for i=1:length(X_P)
                plot(X_P(i,1),X_P(i,2),'k.');hold on;
            end
            deg_sign=char(0176);
            %                         [IDX, C] = kmeans(X_P, Num_centers,'Distance','cityblock','Replicates',5);
            rad=0.1
            out_center = f_mycenter(X_P,rad);
            % sdf
            C = [out_center.C1; out_center.C2; out_center.C3];
            eval(['load Freq_centers' num2str(freq) ])

            for i=1:length(F_centers)
                s=plot(F_centers(i,1),F_centers(i,2),'go');hold on;set(s,'linewidth',2)
            end
            ss=text(0.6,-0.8,[num2str(freq) ' Hz']);set(ss,'fontsize',14,'color','r')

            plot(C(1,1),C(1,2),'bo','Markersize',20,'LineWidth',2);hold on;
            plot(C(2,1),C(2,2),'mo','Markersize',20,'LineWidth',2);hold on;
            plot(C(3,1),C(3,2),'ro','Markersize',20,'LineWidth',2);hold on;
            grid on
            set(gca,'Xtick',-1:0.5:1)
            set(gca,'Ytick',-1:0.5:1)
            axis([-1 1 -1 1])
            hold on;
            plot(-1:1,[0,0,0],'k', 'LineWidth', 0.5);
            plot([0,0,0],-1:1,'k','LineWidth',0.5)
            set(gca,'fontsize',10)
            xlabel('Real','fontsize',12)
            ylabel('Imaginary','fontsize',12)
            hold on
            % now plot the line:
            plot(x_spiral,y_spiral,'color',[0.5 0.5 0.5]) ; % nturns crossings, including end point
            axis square
            out = f_spiral_classification(C(1,:),x_spiral,y_spiral)
            fine_angles = linspace(true_angle(1), true_angle(end), length(x_spiral))
            classified_angle(1)= round(fine_angles(out.class))

            out = f_spiral_classification(C(2,:),x_spiral,y_spiral)
            fine_angles = linspace(true_angle(1), true_angle(end), length(x_spiral))
            classified_angle(2)= round(fine_angles(out.class))
            %             record_class{icond}(ifreql,iangle)=classified_angle;

            % title(['True: ' num2str(out_table(iangle,2)) '&' num2str(out_table(iangle,3)) '; Classi:' num2str(classified_angle(1)) '&' num2str(classified_angle(2))])
           title(['True: ' (out_table(iangle,2)) '&' (out_table(iangle,3)) '; Classi:' num2str(classified_angle(1)) '&' num2str(classified_angle(2))])
             figure(f2)
            subplot(2,2,ifreql)
            plot(out_table(iangle,2), out_table(iangle,3),'r*');hold on;
            plot([out_table(iangle,2) classified_angle(1) ],[out_table(iangle,3) classified_angle(2)],'k-');hold on;
            axis([-100 100 -100 100])
            title([num2str(freq) ' Hz']);


            pause(0.01)
            % pause
        end
    end
end


% figure;
% pc = 'bkrm';
% for ifreq = 1:4
%     s=plot([fliplr(-Angles) Angles],[fliplr(record_class{1}(ifreq,:)) record_class{2}(ifreq,:)]',pc(ifreq));hold on;
%     if ifreq>2
%         set(s,'linewidth',2)
%     end
% end
% plot([-100 100],[-100 100],'g');
% legend('300 Hz',' 600 Hz',' 1500 Hz',' 3000 Hz','Perfect','Location','northwest')
% legend boxoff
% xlabel('True Angle (degrees)','fontsize',12)
% ylabel('classified Angle (degrees)','fontsize',12)
